package com.easylinkin.linkappapi.lobar.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.lobar.entity.EnUserGateLink;
import com.easylinkin.linkappapi.lobar.entity.Gate;
import com.easylinkin.linkappapi.lobar.service.EnUserGateLinkService;
import com.easylinkin.linkappapi.operatelog.LogHelper;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * @author: kan yuanfeng
 * @Date: 2020/11/04 11:42
 * @Description: 管理人员管理人员闸机关联关联
 */
@RestController
@RequestMapping("enUserGateLink")
@Api(tags = "管理人员管理人员闸机关联关联")
public class EnUserGateLinkController {
  
  @Autowired
  private EnUserGateLinkService enUserGateLinkService;
  @Autowired
  private RedisTemplate redisTemplate;

  /**
   * @Description: 增加管理人员闸机关联
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping
  @ApiOperation("增加管理人员闸机关联")
  public RestMessage insert(@RequestBody EnUserGateLink enUserGateLink){
    //todo 参数验证
    enUserGateLinkService.insert(enUserGateLink);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 删除管理人员闸机关联（包含批量删除）
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @DeleteMapping
  @ApiOperation("删除管理人员闸机关联（包含批量删除）")
  public RestMessage delBatch(@RequestBody EnUserGateLink enUserGateLink){
    Assert.notEmpty(enUserGateLink.getIds(),"id不能为空");
    enUserGateLinkService.removeByIds(enUserGateLink.getIds());
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 编辑管理人员闸机关联
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PutMapping
  @ApiOperation("编辑管理人员闸机关联")
  public RestMessage updateById(@RequestBody EnUserGateLink enUserGateLink){
    Assert.notNull(enUserGateLink.getId(),"id不能为空");
    enUserGateLinkService.updateOne(enUserGateLink);
    return RestBuilders.successBuilder().build();
  }

  /**
   * @Description: 根据id查询管理人员闸机关联详情
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @GetMapping("{id}")
  @ApiOperation("根据id查询管理人员闸机关联详情")
  public RestMessage findById(@PathVariable("id")Long id) {
    Assert.notNull(id,"id不能为空");
    EnUserGateLink enUserGateLink = enUserGateLinkService.getById(id);
    return RestBuilders.successBuilder().data(enUserGateLink).build();
  }

  /**
   * @Description: 根据条件，分页(不分页)查询
   * @author kan yuanfeng
   * @date 2020/11/04 11:42
   */
  @PostMapping("list")
  @ApiOperation("根据条件，分页(不分页)查询")
  public RestMessage queryListByPage(@RequestBody RequestModel<EnUserGateLink> requestModel){
    Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
    Assert.notNull(requestModel.getPage(), "page 不能为空");
    IPage<EnUserGateLink> record =  enUserGateLinkService.queryListByPage(requestModel);
    return RestBuilders.successBuilder().data(record).build();
  }

  /**
   * 同步授权人员
   * @param gate
   */
  @PostMapping("upUserToGate")
  @ApiOperation("同步授权人员")
  public RestMessage upUserToGate(@RequestBody Gate gate){
    Assert.notNull(gate.getId(),"id不能为空");
    enUserGateLinkService.upUserToGate(gate.getId());
    return RestBuilders.successBuilder().build();
  }

  /**
   * 查询下发闸机结果
   * @param id
   */
  @GetMapping("result")
  @ApiOperation("查询下发闸机结果")
  public RestMessage getResult(String id){
    Assert.notNull(id,"id不能为空");
    Object o = redisTemplate.opsForValue().get(EnUserGateLink.getRedisIdPre() + id);
    return RestBuilders.successBuilder().data(o).build();
  }
}

